
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#pragma once

#include <math.h>
#include <iostream>
using std::cout;
#include "CMatrix.h"

//3 floats vector class
class CVector3f
{
public:
	float x, y, z;

public:
	CVector3f():x(0), y(0), z(0){};
	CVector3f(float x_, float y_, float z_):x(x_), y(y_), z(z_){};

	virtual ~CVector3f(){};


	CVector3f operator +(const CVector3f& v)const;
	CVector3f operator -(const CVector3f& v)const;
	CVector3f operator -()const;
	CVector3f operator *(float a)const;
	CVector3f operator *(const CVector3f& v)const;
	CVector3f operator *(const CMatrix& M)const;
	CVector3f operator /(float a)const;
	void operator += (const CVector3f& v);
	void operator *= (float f);
	bool operator == (const CVector3f& v);
	bool operator != (const CVector3f& v);

	float dot(const CVector3f& v)const;
	CVector3f cross(const CVector3f& v)const;

	float norme()const;
	float norme2()const;

	void normalize();

	void saturate();
	void maximize(float max);

	void display()const;

	void setByID(int id, float value);
};

//add two vectors
inline CVector3f CVector3f::operator +(const CVector3f& v)const
{
	CVector3f result(x + v.x, y + v.y, z + v.z);
	return result;
}

//substraction of two vectors
inline CVector3f CVector3f::operator -(const CVector3f& v)const
{
	CVector3f result(x - v.x, y - v.y, z - v.z);
	return result;
}

//unary minus operator
inline CVector3f CVector3f::operator -()const
{
	return CVector3f(-x, -y, -z);
}

//multiply a vector with a float
inline CVector3f CVector3f::operator *(float a)const
{
	CVector3f result(x * a, y * a, z * a);
	return result;
}

//multiply two vectors
inline CVector3f CVector3f::operator *(const CVector3f& v)const
{
	CVector3f result(x*v.x, y*v.y, z*v.z);
	return result;
}

//multiply a vector with a matrix
inline CVector3f CVector3f::operator *(const CMatrix& M)const
{
	CVector3f res;

	res.x = x * M.m_array[0][0] + y * M.m_array[1][0] + z * M.m_array[2][0] + M.m_array[3][0];
	res.y = x * M.m_array[0][1] + y * M.m_array[1][1] + z * M.m_array[2][1] + M.m_array[3][1];
	res.z = x * M.m_array[0][2] + y * M.m_array[1][2] + z * M.m_array[2][2] + M.m_array[3][2];

	return res;
}

//divide a vector by a float
inline CVector3f CVector3f::operator /(float a)const
{
	a = 1.f/a;

	return CVector3f(x * a,	y * a, z * a);
}

//add and assign a vector
inline void CVector3f::operator += (const CVector3f& v)
{
	x+=v.x;
	y+=v.y;
	z+=v.z;
}

//multiply by a float and assign
inline void CVector3f::operator *= (float f)
{
	x *= f;
	y *= f;
	z *= f;
}

//equal test
inline bool CVector3f::operator == (const CVector3f& v)
{
	if(x == v.x && y == v.y && z == v.z)
		return true;

	return false;
}

//unequal test
inline bool CVector3f::operator != (const CVector3f& v)
{
	if(x!=v.x || y!=v.y || z!=v.z)
		return true;

	return false;
}

//return the dot product of two vectors
inline float CVector3f::dot(const CVector3f& v)const
{
	return x*v.x + y*v.y + z*v.z;
}

//return the cross product of two vectors
inline CVector3f CVector3f::cross(const CVector3f& v)const
{
	CVector3f result(
		y*v.z - z*v.y,
		z*v.x - x*v.z,
		x*v.y - y*v.x);
	return result;

}

//return the norme of a vector
inline float CVector3f::norme()const
{
	return sqrt(x*x + y*y + z*z);
}

//return the quare of the norme
inline float CVector3f::norme2()const
{
	return x*x + y*y + z*z;
}

//normalize the vector
inline void CVector3f::normalize()
{
	float n = norme();
	if(n == 0) return;
	x /= n;
	y /= n;
	z /= n;
}

//saturation between 0 and 1
inline void CVector3f::saturate()
{
	if(x<0) x = 0;
	else if(x>1) x = 1;

	if(y<0) y = 0;
	else if(y>1) y = 1;

	if(z<0) z = 0;
	else if(z>1) z = 1;
}

//set the vector to a sepcific length (the vector needs to be normalized)
inline void CVector3f::maximize(float max)
{
	x *= max;
	y *= max;
	z *= max;
}

//display the vector using cout
inline void CVector3f::display()const
{
	cout<<x<<" "<<y<<" "<<z;
}

//set a component ofthe vector using an id (0 = x, 1 = y, z = 2)
inline void CVector3f::setByID(int id, float value)
{
	switch(id)
	{
		case 0:
			x = value;
			break;

		case 1:
			y = value;
			break;

		case 2:
			z = value;
			break;

		default:
			break;
	}
}